<?php
namespace Home\Common;

class Redis {
	protected $redis;
	function __construct() {
		if (! isset ( $GLOBALS ['redis'] )) {
			$GLOBALS ['redis'] = new \Redis ();
			$GLOBALS ['redis']->connect ( "127.0.0.1", 6379 );
			$GLOBALS ['redis']->select ( 10 );
		}
		$this->redis = $GLOBALS ['redis'];
	}
	
	/* 用户注册 */
	function toSignup($data) {
		if ($this->redis->hexists ( "email:to:userid", $data ['email'] ))
			return 10;
		
		$userid = $this->redis->incr ( "user:count" );
		$userInfo = array (
				"email" => $data ['email'],
				"password" => $data ['password'],
				"linkcount" => 0 
		);
		$this->redis->hmset ( "user:$userid", $userInfo );
		$status = $this->redis->hset ( "email:to:userid", $data ['email'], $userid );
		if ($status) {
			session ( 'nwb_islogin', 1 );
			session ( 'nwb_userid', $userid );
			session ( 'nwb_email', $userInfo ['email'] );
			
			/* 日志生成 */
			$logContent = session ( 'nwb_userid' ) . "," . session ( 'nwb_email' ) . ",toSignUp," . $data ['password'];
			writeLog ( $logContent, 'signup' );
		}
		return $status ? 1 : 16;
	}
	
	/* 用户登录 */
	function toLogin($data) {
		$userid = $this->redis->hget ( "email:to:userid", $data ['email'] );
		if (! $this->redis->hExists ( "email:to:userid", $data ['email'] ))
			return 14;
		
		$password = $this->redis->hget ( "user:$userid", "password" );
		if ($data ['password'] == $password) {
			session ( 'nwb_islogin', 1 );
			session ( 'nwb_userid', $userid );
			session ( 'nwb_email', $data ['email'] );
			session ( 'nwb_password', $data ['password'] );
			session ( 'nwb_usercen', $this->redis->lrange ( "user:{$userid}:cen", 0, - 1 ) );
			
			/* 日志生成 */
			$logContent = session ( 'nwb_userid' ) . "," . session ( 'nwb_email' ) . ",toLogin";
			writeLog ( $logContent, 'login' );
			
			return 1;
		} else {
			return 12;
		}
	}
	
	/* 网站一级分类获取 */
	function getType1Link($type) {
		$public_link_set = $this->redis->keys ( "public:link:[0-9]*" );
		
		$public_link_info = array ();
		foreach ( $public_link_set as $a_link_key ) {
			$a_link_info = $this->redis->hGetAll ( $a_link_key );
			if ($a_link_info ['type1'] == $type)
				array_push ( $public_link_info, $a_link_info );
		}
		
		/*排序*/
		$pris = array();
		foreach($public_link_info as $a_link_info){
			$pris[] = $a_link_info['linkhead'];
		}
		array_multisort($pris,SORT_ASC,$public_link_info);
		
		return $public_link_info;
	}
	
	/* 网站二级分类获取 */
	function getType2Link($type) {
		$public_link_set = $this->redis->keys ( "public:link:[0-9]*" );
		
		$public_link_info = array ();
		foreach ( $public_link_set as $a_link_key ) {
			$a_link_info = $this->redis->hGetAll ( $a_link_key );
			if ($a_link_info ['type2'] == $type)
				array_push ( $public_link_info, $a_link_info );
		}
		
		/*排序*/
		$pris = array();
		foreach($public_link_info as $a_link_info){
			$pris[] = $a_link_info['linkhead'];
		}
		array_multisort($pris,SORT_ASC,$pris);
		
		return $public_link_info;
	}
	
	/* 公有链接获取 */
	function getPublicLink() {
		$public_link_set = $this->redis->keys ( "public:link:[0-9]*" );
		$words = array (
				"#","a","b","c","d","e","f","g","h","i","g","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" 
		);
		foreach ( $words as $aword ) {
			$public_link_info [$aword] = array ();
		}
		
		foreach ( $public_link_set as $public_link ) {
			$a_link_info = $this->redis->hGetAll ( $public_link );
			if (in_array ( $a_link_info ['linkhead'], array (
					"0","1","2","3","4","5","6","7","8","9"
			) ))
				$a_link_info ['linkhead'] = "#";
			array_push ( $public_link_info [$a_link_info ['linkhead']], $a_link_info );
		}
		
		/*优先级排序 linkpri*/
		foreach($words as $aword){
			$pris = array();
			foreach($public_link_info[$aword] as $a_public_link){
				$pris[] = $a_public_link['linkpri'];
			}
			array_multisort($pris,SORT_ASC,$public_link_info[$aword]);
		}
		
		return $public_link_info;
	}
	
	/* 私有链接获取 */
	function getPrivateLink() {
		$userid = session ( 'nwb_userid' );
		$private_link_set = $this->redis->keys ( "user:{$userid}:link:[0-9]*" );
		$private_link_info = array ();
		
		foreach ( $private_link_set as $private_link ) {
			$a_link_info = $this->redis->hGetAll ( $private_link );
			array_push ( $private_link_info, $a_link_info );
		}
		foreach ( session ( 'nwb_usercen' ) as $pubLinkId ) {
			$a_link_info = $this->redis->hGetAll ( "public:link:{$pubLinkId}" );
			array_push ( $private_link_info, $a_link_info );
		}
		return $private_link_info;
	}
	
	/* 私有链接添加	linkname linkurl linkicon linkabc */
	function addPrivateLink($data) {
		$userid = session ( 'nwb_userid' );
		$linkid = $this->redis->hIncrBy ( "user:{$userid}", "linkcount", 1 );
		$data ['linkid'] = $linkid;
		
		$status = $this->redis->hMset ( "user:{$userid}:link:{$linkid}", $data );
		
		return $status ? true : false;
	}
	
	/* 移除私有链接 */
	function delPrivateLink($data) {
		$userid = session ( 'nwb_userid' );
		$linkid = $data ['linkid'];
		
		if (in_array ( $linkid, session ( 'nwb_usercen' ) )) {
			$status = $this->redis->lRemove ( "user:{$userid}:cen", $linkid, 0 );
			session ( 'nwb_usercen', $this->redis->lrange ( "user:{$userid}:cen", 0, - 1 ) );
			return $status ? true : false;
		} else {
			$status = $this->redis->del ( "user:{$userid}:link:{$linkid}" );
			return $status ? true : false;
		}
	}
	
	/* 公有链接至私有 */
	function cenPublicLink($data) {
		$pubLinkId = $data ['pubLinkId'];
		$userid = session ( 'nwb_userid' );
		
		if (! in_array ( $pubLinkId, session ( 'nwb_usercen' ) )) {
			$status = $this->redis->lPush ( "user:{$userid}:cen", $pubLinkId );
			session ( 'nwb_usercen', $this->redis->lrange ( "user:{$userid}:cen", 0, - 1 ) );
			
			return $status ? true : false;
		} else {
			$status = $this->redis->lRemove ( "user:{$userid}:cen", $pubLinkId, 0 );
			session ( 'nwb_usercen', $this->redis->lrange ( "user:{$userid}:cen", 0, - 1 ) );
			
			return $status ? true : false;
		}
	}
	/* 密码更改 */
	function rePwd($data) {
		$userid = session ( 'nwb_userid' );
		$password = $this->redis->hGet ( "user:{$userid}", "password" );
		
		if ($password == $data ['old_pwd']) {
			$status = $this->redis->hSet ( "user:{$userid}", "password", $data ['new_pwd'] );
			
			if(!status) return 24;
			
			/* 日志生成 */
			$logContent = session ( "nwb_userid" ) . "," . session ( "nwb_email" ) . ",rePwd," . $data['old_pwd'] . "," . $data['new_pwd'];
			writeLog ( $logContent, 'userOps' );
			
			return 1;
		} else {
			return 22;
		}
	}
	function __destruct() {
		$this->redis = NULL;
	}
}